package org.sgx.gwteditors.testgallery.gallery;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.basetypes.Color;
import org.sgx.gwteditors.client.impl1.basetypes.InRangeDouble;
import org.sgx.gwteditors.client.impl1.basetypes.SelectionList;
import org.sgx.gwteditors.client.impl1.complex.Array1DEditor3;
import org.sgx.gwteditors.client.impl1.complex.Array1DEditor3.ItemCreator;
import org.sgx.gwteditors.client.impl1.edbean.EditableBean;
import org.sgx.gwteditors.client.util.TestUtil;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ExternalTextResource;
import com.google.gwt.user.client.ui.Panel;

/**
 * Using of Array1DEditor3 for editing a List of objects
 * 
 * @author sg
 * 
 */
public class Array1DEditor2Test1 implements Test {

	@Override
	/*---begintestmaincode---*/
	public void test(Panel parent) {
		new EditorFramework1().start(); 
		
		//we want to define a list of three persons (we will let the user to edit this list) - for that we use a ItemCreator helper class - not neccessary
		
		final SelectionList<String> categories = new SelectionList<String>(new String[] { "defense", "attack", "creation",
				"energy" }, new String[] { "defense" });

		ItemCreator<Person1> creator = new ItemCreator<Person1>() {
			@Override
			public Person1 createNewItem() {
				Person1 p1 = GWT.create(Person1.class);
				p1.getProperties().reorder("name", "age", "male", "eyeColor", "category", "size"); 
				p1.setName("Unammed Person");
				p1.setAge(28);
				p1.setMale(true);
				p1.setEyeColor(new Color(0, 222, 0));
				p1.setCategory(categories);
				p1.setSize(new InRangeDouble(20, 80, 40));
				return p1; 
			}
		};
		
		Person1 p1 = creator.createNewItem();
		p1.setName("Sebastián");
		p1.setAge(28);
		p1.setMale(true);
		p1.setEyeColor(new Color(255, 0, 0));
		p1.setCategory(categories);
		p1.setSize(new InRangeDouble(20, 80, 40));

		Person1 p2 = creator.createNewItem();
		p2.setName("Laura");
		p2.setAge(26);
		p2.setMale(true);
		p2.setEyeColor(new Color(255, 220, 0));
		p2.setCategory(categories);
		p2.setSize(new InRangeDouble(120, 180, 140));
		
		Person1 p3 =creator.createNewItem();
		p3.setName("Andrés");
		p3.setAge(21);
		p3.setMale(true);
		p3.setEyeColor(new Color(25, 220, 222));
		p3.setCategory(categories);
		p3.setSize(new InRangeDouble(120, 180, 140));

		Array1DEditor3.Namer<Person1> namer = new Array1DEditor3.Namer<Person1>() {
			@Override
			public String getName(Person1 t) {
				return t.getName();
			}
		};
		
		Array1DEditor3<Person1> ed3 = new Array1DEditor3<Person1>(
				EditorFramework1.TYPE_PROPERTYHAVER, namer, creator);
		
		//the user can modify the list himself so the list must not be an Unmodifiable one (cannot use Arrays.asList())
		List<Person1> peopleList = new LinkedList<Person1>();		
		peopleList.add(p1);
		peopleList.add(p2); 
		peopleList.add(p3);
		
		ed3.load(peopleList);
		parent.add(ed3);
	}
	

	//The bean we want to edit

	public static abstract class Person1 implements EditableBean {		
		String name;
		Integer age;
		Boolean male;
		Color eyeColor, nullColor;
		SelectionList<String> category;
		Person1 mother;
		InRangeDouble size; 	
		
		public Person1(){}
		public String getName() {
			return name;
		}		
		public void setName(String name) {
			this.name = name;
		}
		public Integer getAge() {
			return age;
		}
		public void setAge(Integer age) {
			this.age = age;
		}
		public Boolean getMale() {
			return male;
		}
		public void setMale(Boolean male) {
			this.male = male;
		}
		public Color getEyeColor() {
			return eyeColor;
		}
		public void setEyeColor(Color eyeColor) {
			this.eyeColor = eyeColor;
		}
		public SelectionList<String> getCategory() {
			return category;
		}
		public void setCategory(SelectionList<String> category) {
			this.category = category;
		}
		public Color getNullColor() {
			return nullColor;
		}
		public void setNullColor(Color nullColor) {
			this.nullColor = nullColor;
		}
		public InRangeDouble getSize() {
			return size;
		}
		public void setSize(InRangeDouble size) {
			this.size = size;
		}
		public Person1 getMother() {
			return mother;
		}
		public void setMother(Person1 mother) {
			this.mother = mother;
		}		
		@Override
		public String toString() {
			return "Person "+getName()+", eyes: "+getEyeColor();
		}	
	}

/*---endtestmaincode---*/

	// test stuff
	@Override
	public String getName() {
		return "array editor 2";
	}

	@Override
	public Map<String, ExternalTextResource> getJavaSources() {
		return TestUtil.getResource(getName(), TestResources.INSTANCE.Array1DEditor2Test1());
	}

	@Override
	public String[] getTags() {
		return new String[] {};
	}

	@Override
	public String getDescription() {
		return "Using of Array1DEditor3 for editing a List of objects";
	}
}
